package cs5300;

import java.util.Calendar;
import java.util.StringTokenizer;

import BasicDataType.CookieType;

public class sessionState {
	    public CookieType cookie;
	    static int SESSIONDISCARDTIME = 10000;
	    public long discard_time;
	    public static int SESSION_TIMEOUT_SECS;
		//static int SESSIONID = 0;
		//public int version;
		public String message;
		//public int SesssionID;
		//public long expireDate;
		/**
		 * generate a global unique sessionID
		 */
		public sessionState() {
			//Calendar cal = Calendar.getInstance();
			//SesssionID = (int) (SESSIONID++ * cal.getTimeInMillis());
			//version = 0;
			//message = "";
			//expireDate = 0;
			discard_time = getdisCardTime();
			message = "hello User";
		}
		
		
		/**
		 * Update the expireTime
		 * @param seconds
		 */
		public static long getdisCardTime(){
			Calendar cal = Calendar.getInstance();
			return cal.getTimeInMillis() + SESSIONDISCARDTIME * 1000;
		}
		
		/**
		 * change the sessionState into a String
		 * @return
		 */
		public String getString(){
			return cookie.getString() + "~" + Long.toString(discard_time) + "~" + message + "~";
		}
		
		/**
		 * generate a sessionState from a String
		 * @param sesString
		 * @return
		 */
		public static sessionState getSessionState(String sesString){
			sessionState ss = new sessionState();
			StringTokenizer st = new StringTokenizer(sesString,"~");
			ss.cookie = new CookieType(st.nextToken());
			ss.discard_time = Long.parseLong(st.nextToken());
			ss.message = st.nextToken();
			return ss;
		}
}
